using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MicroStar
{
    /// <summary>
    /// Particle class
    /// </summary>
    public class Particle
    {
        private Vector2 origin = Vector2.Zero;

        ///<summary>
        /// Constructor
        ///</summary>
        public Particle()
        {
            Position = Vector2.Zero;
            Scale = Vector2.One;
            Color = Color.White;
            Velocity = Vector2.Zero;
            Alpha = 1f;
        }

        internal void Update(double elapsedMilliseconds)
        {
            if(origin == Vector2.Zero)
                origin = new Vector2(Texture.Width / 2f, Texture.Height / 2f);

            Position += Velocity;

            if (Lifespan < (elapsedMilliseconds - Inception))
                IsAlive = false;
        }

        internal void Affect(ref Vector2 attraction)
        {
            Velocity = Vector2.Add(Velocity, attraction);
        }

        internal void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, Position, null, Color * Alpha, Rotation, origin, Scale, SpriteEffects.None, 0);
        }

        internal Texture2D Texture { get; set; }
        internal float Inception { get; set; }
        internal int Lifespan { get; set; }
        internal bool IsAlive { get; set; }
        internal Color Color { get; set; }
        internal float Alpha { get; set; }
        internal Vector2 Position { get; set; }
        internal float Rotation { get; set; }
        internal Vector2 Scale { get; set; }
        internal Vector2 Velocity { get; set; }

        internal Vector2 ProjectedPosition
        {
            get { return Position + Velocity; }
        }
    }
}
